package com.ma.workorder;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.ma.R;
import com.ma.databinding.ActivityOrderInfoBinding;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import base.BaseActivity;
import bean.LoginBean;
import database.dao.DBManager;
import database.table.Dbb;
import database.table.Gdb;
import database.table.Jdb;
import httpUtil.Api;
import httpUtil.Http;
import lomasky.xui.BaseViewHolder;
import lomasky.xui.BaseViewHolderForListView;
import lomasky.xui.CommonAdapter;
import lomasky.xui.ItemDivider;
import lomasky.xui.RecyclerViewAdapter;
import lomasky.xui.WrapContentLinearLayoutManager;
import utils.ConstantUtil;
import utils.JsonUtil;
import utils.StringUtil;

public class OrderInfoActivity extends BaseActivity {
    int POSITION;
    ActivityOrderInfoBinding binding;
    private Gdb gdb;
    Dbb ddb;
    private LoginBean loginBean;
    RecyclerViewAdapter<Dbb> dbbAdapter;
    boolean isSelf;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        gdb = (Gdb) getIntent().getSerializableExtra(ConstantUtil.DATA2);
        loginBean = (LoginBean) getIntent().getSerializableExtra(ConstantUtil.DATA1);
        POSITION = getIntent().getIntExtra(ConstantUtil.POSITION, -1);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_order_info);
        binding.setGdBean(gdb);
        binding.setUserBean(loginBean);
        isSelf=loginBean.getID().equals(gdb.getGDB_FQRWJ());
        binding.setIsSelf(isSelf);
        dbbAdapter = new RecyclerViewAdapter<Dbb>(this, R.layout.dbb_list_item) {
            @Override
            public void onBind(BaseViewHolder holder, Dbb item, int position) {
                holder.setText(R.id.name, item.getDBB_DBR());
                holder.setText(R.id.time, StringUtil.formatTime(item.getDBB_DBSJ()));
                holder.setText(R.id.text, item.getDBB_DBLR());
            }


        };
        binding.listView.setLayoutManager(new WrapContentLinearLayoutManager(this));
        binding.listView.addItemDecoration(new ItemDivider(this));
        binding.listView.setAdapter(dbbAdapter);
        if (gdb.getGDB_SFDB()) {
            getDB(gdb.getGDB_ZJ());
        }
    }

    public void ok(View view) {

        showProgressDialog();
        gdb.setGDB_SFJD(true);
        DBManager.get().getGdbDao().insertOrReplace(gdb);
        Map<String, Object> map = new HashMap<>();
        map.put("JDB_GDBWJ", gdb.getGDB_ZJ());
        map.put("JDB_JDR", loginBean.getRealName());
        map.put("JDB_JDRWJ", loginBean.getID());
        Http.post(Api.AddJDB, map)
                .subscribe(res -> {
                    if (res.getCode() == 200) {
                        Jdb jdb = JsonUtil.getObject(res.getContent(), Jdb.class);
                        DBManager.get().getJdbDao().insertOrReplace(jdb);

                    } else {
                        toast("接单失败了");
                    }

                    closeProgressDialog();
                }, throwable -> {

                    closeProgressDialog();
                    toast("接单失败了");
                });

    }

    public void close(View view) {

        showCustomDialog("关闭工单", "是否关闭本工单", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showProgressDialog();
                gdb.setGDB_SFJD(true);
                DBManager.get().getGdbDao().insertOrReplace(gdb);
                Map<String, Object> map = new HashMap<>();
                map.put("GDB_FQRWJ", loginBean.getID());
                map.put("GDB_ZJ", gdb.getGDB_ZJ());
                Http.post(Api.CloseGDB, map)
                        .subscribe(res -> {
                            if (res.getCode() == 200) {
                                Intent intent = new Intent();
                                intent.putExtra(ConstantUtil.POSITION,POSITION);
                                intent.putExtra(ConstantUtil.ACTION,ConstantUtil.DEL);
                                setResult(RESULT_OK,intent);
                                finish();

                            } else {
                                toast("接单失败了");
                            }

                            closeProgressDialog();
                        }, throwable -> {

                            closeProgressDialog();
                            toast("接单失败了");
                        });

            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

    }

    public void play(View view) {


    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void dubanok(int ok){
        if (ok==1){
            Intent intent = new Intent();
            intent.putExtra(ConstantUtil.POSITION,POSITION);
            intent.putExtra(ConstantUtil.ACTION,ConstantUtil.EDIT);
            setResult(RESULT_OK,intent);
            finish();
        }
    }

    private void getDB(String gdb_zj) {
        Map<String, Object> map = new HashMap<>();
        map.put("DBB_GDBWJ", gdb_zj);
        map.put("DBB_DBRWJ", loginBean.getID());
        Http.post(Api.GetDBBByGBDZJ, map)
                .subscribe(res -> {
                    List<Dbb> list = JsonUtil.getList(res.getContent(), Dbb.class);
                    dbbAdapter.setData(list);
                    dbbAdapter.notifyDataSetChanged();

                }, throwable -> {

                });

    }


    public void duban(View view) {


        SuperviseDialog dialog = new SuperviseDialog(this, loginBean, gdb);
        dialog.show();


    }
}
